package com.yongcheon.sms_manager.ui.dialogs;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.activities.SpamNumManagementActivity;
import com.yongcheon.sms_manager.ui.phone_spam.list.PhoneLongClickListener;
import com.yongcheon.sms_manager.ui.preference.SpamNumLongClickListener;
import com.yongcheon.sms_manager.ui.sms_spam.list.SMSLongClickListener;
import com.yongcheon.sms_manager.utils.StringUtil;
import com.yongcheon.sms_manager.vo.SpamNumVO;

/**
 * Created by yongcheon on 2014-12-12.
 */
public class SpamRegistDlg {

    public static void showSpamRegistDlg(final Context context, View view, final SpamNumVO spamNumVO, final Object callObj, boolean isAddresssEdit) {
        final LinearLayout linear = (LinearLayout) view.inflate(context, R.layout.spam_regist, null);
        final EditText address = (EditText)linear.findViewById(R.id.phone_number);
        address.setText(spamNumVO.getAddress());
        if(isAddresssEdit == true) address.setEnabled(true);

        final Spinner spamTypeSpin = (Spinner)linear.findViewById(R.id.spam_type);
        ArrayAdapter<CharSequence> adspin = ArrayAdapter.createFromResource(context, R.array.spam_type, android.R.layout.simple_spinner_item);
        adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spamTypeSpin.setAdapter(adspin);
        if(spamNumVO.getCategory_2() != null){
            int idx = StringUtil.getIndexInStringArray(context.getResources().getStringArray(R.array.spam_type), spamNumVO.getCategory_2());
            idx = (idx == -1) ? 0 : idx;
            spamTypeSpin.setSelection(idx);
        }

        final EditText ed_subject = (EditText) linear.findViewById(R.id.spam_subject);
        if(spamNumVO.getSubject() != null){
            ed_subject.setText(spamNumVO.getSubject());
        }

        new AlertDialog.Builder(context)
                .setTitle(context.getResources().getString(R.string.register_user_spam))
                .setView(linear)
                .setPositiveButton(context.getResources().getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String spamType = spamTypeSpin.getSelectedItem().toString();
                        String subject = ed_subject.getText().toString();
                        if (subject == null || subject.trim().equals(""))
                            subject = context.getResources().getString(R.string.unregist_desc);
                        spamNumVO.setAddress(address.getText().toString());
                        spamNumVO.setCategory_2(spamType);
                        spamNumVO.setSubject(subject);

                        if (callObj instanceof SpamNumLongClickListener.SpamDialogOnClickListener) {
                            SpamNumLongClickListener.SpamDialogOnClickListener listener = (SpamNumLongClickListener.SpamDialogOnClickListener) callObj;
                            listener.editSpam(spamNumVO);
                        } else if (callObj instanceof SMSLongClickListener.SMSDialogOnClickListener) {
                            SMSLongClickListener.SMSDialogOnClickListener listener = (SMSLongClickListener.SMSDialogOnClickListener) callObj;
                            if (spamNumVO.getEtc() != null && spamNumVO.getEtc().equals("1")) {
                                listener.registOnlyOneSpam(spamNumVO);
                            }else {
                                listener.registAllSpam(spamNumVO);
                            }
                        } else if (callObj instanceof SpamNumManagementActivity) {
                            SpamNumManagementActivity spamNumManagementActivity = (SpamNumManagementActivity) callObj;
                            spamNumManagementActivity.registSpam(spamNumVO);
                        }else  if (callObj instanceof PhoneLongClickListener.PhoneDialogOnClickListener) {
                            PhoneLongClickListener.PhoneDialogOnClickListener listener = (PhoneLongClickListener.PhoneDialogOnClickListener) callObj;
                            listener.registAllSpam(spamNumVO);
                        }
                    }
                })
                .setNegativeButton(context.getResources().getString(R.string.cancel), null)
                .show();
    }

}
